package view
{
	import data.FlyProp;
	import data.Globals;
	import data.TankProp;
	import data.YaLuJiProp;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import manager.EffectName;
	import manager.MapConfig;
	import manager.ObjDataManage;
	import manager.ObjectPool;
	import manager.TankName;
	
	import view.Arm.BasePlane;
	import view.Arm.BaseTank;
	import view.Arm.tank.CTank;
	import view.base.ClearView;
	import view.other.YaLuJiView;
	
	public class TankView extends ClearView
	{
		public var tankAry:Array;
	
		private var yaluji:YaLuJiView = new YaLuJiView();
		
		private var curDestoryTank:BaseTank;
		public function TankView()
		{
			super();
			tankAry = [];
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
			this.addEventListener(MouseEvent.MOUSE_OVER,overHandle);
			this.addEventListener(MouseEvent.MOUSE_OUT,outHandle);
		}
		
		protected function overHandle(event:MouseEvent):void
		{
			var tank:BaseTank = event.target.parent as BaseTank;
			if(tank){
				Globals.gameView.showTankTip(true,tank.x,tank.y,tank.blood,tank.shoutRadius,tank.rotateSpeed,tank.shoutGap);
			}
		}
		
		protected function outHandle(event:MouseEvent):void
		{
			Globals.gameView.showTankTip(false);
		}
		
		public function initface():void{
			initYalujiView();
		}
		
		private function mouseUp(e:MouseEvent):void{
			if(e.target.parent is BaseTank&&Globals.gameView.getCurArmType()==TankName.YALUJI){
				curDestoryTank = e.target.parent as BaseTank;
				productionTank(TankName.YALUJI,new Point(curDestoryTank.x,curDestoryTank.y));
			}
			
		}
		
		private function initYalujiView():void{
			if(!yaluji)yaluji = new YaLuJiView();
		}
		
		override public function clearView():void{
			super.clearView();
			while(tankAry.length){
				var tank:BaseTank = tankAry.shift();
				tank.isCloseLoop = true;
				tank.shoutTarget = null;
				ObjectPool.putObj(tank);
			}
		
		}
		
		override protected function loop(event:Event):void{
			
		}
		/**
		 * 生产坦克 
		 * @param type 坦克类型
		 * @param p 坐标 Point
		 * 
		 */
		public function productionTank(type:String,p:Point):void{
			if(type==TankName.YALUJI){
				var yaluji:YaLuJiView = ObjectPool.getObj(TankName.getTankCls(type)) as YaLuJiView;
				var yProp:YaLuJiProp = ObjDataManage.getInstance().getTankProp(type) as YaLuJiProp;
				yaluji.show(p.x,p.y,yalujiCall);
				this.addChild(yaluji);
				yaluji.name = yProp.name;
				Globals.gameView.calculateScore(-yProp.revive);
			}else{
				var tank:BaseTank = ObjectPool.getObj(TankName.getTankCls(type)) as BaseTank;
				this.addChild(tank);
				var tp:TankProp = ObjDataManage.getInstance().getTankProp(type) as TankProp;
				tank.shoutGap = tp.shoutGap;
				tank.shoutRadius = tp.shoutRadius;
				tank.bulletType = tp.bulletType;
				tank.rotateSpeed = tp.rotateSpeed;
				tank.x =p.x;
				tank.y =p.y;
				tank.rotation = 0;
				tank.isCloseLoop = false;
				tank.shoutTarget = null;
				tank.name = tp.name;
				tank.blood = tp.blood;
				if(tank is CTank){
					(tank as CTank).bullet =false;
				}
				if(tank is BasePlane){
					var plane:BasePlane = tank as BasePlane;
				    var fp:FlyProp = tp as FlyProp;
					var p:Point = MapConfig.getInstance().line[MapConfig.getInstance().line.length - 1];
					plane.x = p.x;
					plane.y = p.y;
					plane.b_num = fp.b_num;
					plane.speed = fp.speed;
					plane.rotation = -90;
					plane.killSelfRat = fp.killSelfRat;
				}
				tank.onRemove = remove;
			
				tankAry.push(tank);
				Globals.gameView.calculateScore(-tp.revive);
			}
			
		}
		
		private function remove(tank:BaseTank):void{
			tankAry.splice(tankAry.indexOf(tank),1);
			Globals.gameView.setGradIsCanSite(tank.x,tank.y,true);
		}
		
		private function yalujiCall():void
		{
			curDestoryTank.die();
		}
	}
}